package cn.com.dingtek.genius.manager.server.event;

import cn.com.dingtek.genius.manager.server.repository.db.entity.UserLoginLog;
import cn.com.dingtek.genius.manager.server.repository.db.manager.UserLoginLogManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class LoginEventListener implements ApplicationListener<LoginEvent> {
    @Autowired
    private UserLoginLogManager userLoginLogManager;

    @Override
    public void onApplicationEvent(LoginEvent event) {
        log.info("{}", event);
        System.out.println("========" + Thread.currentThread().getName());

        try {
            userLoginLogManager.save(UserLoginLog.builder()
                    .userId(event.getUserId())
                    .accountName(event.getAccountName())
                    .ip(event.getIp())
                    .agent(event.getAgent())
                    .os(event.getOs())
                    .logState(event.getLogState())
                    .remark(event.getRemark())
                    .ctime(new Date())
                    .build());
        } catch (Exception e) {
            log.error("user login log error.event:{}", event, e);
        }
    }
}
